/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:
 * Date: 2025-04-07
 * Time: 20:44
 */

//懒汉模式
class SingletonLazy{

    private static volatile SingletonLazy instance = null;

    private static Object locker = new Object();

    public static SingletonLazy getInstance() {
        if(instance == null){
            synchronized (locker){
                if(instance == null){
                    instance = new SingletonLazy();
                }
            }
        }
        return instance;
    }

    //第二种加锁方式
    /*public synchronized static SingletonLazy getInstance2() {
            if(instance == null){
                instance = new SingletonLazy();
            }
        return instance;
    }*/

    private SingletonLazy(){

    }
}


public class demo2 {
    public static void main(String[] args) {
        SingletonLazy t1 = SingletonLazy.getInstance();
        SingletonLazy t2 = SingletonLazy.getInstance();
        System.out.println(t1 == t2);
        //SingletonLazy t3 = new SingletonLazy();
    }
}
